//
//  HelpListViewController.swift
//  GhostWorld
//
//  Created by Kam on 2018/8/9.
//  Copyright © 2018 LKGood. All rights reserved.
//

import UIKit
import PKHUD

private let helpCellIdentifier = "HelpCell"
private let noDataCellIdentifier = "NoDataTableCell"

struct HelpTitle {
  var id: Int
  var title: String
}

class HelpListViewController: BaseViewController {
  
  // MARK: - IBOutlets
  
  @IBOutlet private weak var tableView: UITableView!
  
  
  // MARK: - Private vars
  
  private var helpTitles: [HelpTitle]?
  private var selectedHelpTitle: HelpTitle?
  
  // MARK: - Private methods
  
  private func prepareUI() {
    tableView.register(UINib(nibName: noDataCellIdentifier, bundle: nil), forCellReuseIdentifier: noDataCellIdentifier)
  }
  
  private func loadHelpList() {
    API.shared.requestAPI (
      APIURLStrings.getHelpTitles,
      method: .get,
      successCallback: { json in
        if let helpsArray = json["helps"].array {
          self.helpTitles = helpsArray.map {
            HelpTitle(id: $0["id"].intValue, title: $0["title"].stringValue)
          }
          self.tableView.reloadData()
        }
    }) { (message, code) in
      HUD.flash(.label("没有帮助数据"), delay: 2.0)
    }
  }
  
  // MARK: - Public vars
  
  
  
  // MARK: - Lifecycle
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    prepareUI()
    loadHelpList()
  }
  
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ShowHelpDetailSegue" {
      let helpDetailViewController = segue.destination as! HelpDetailViewController
      helpDetailViewController.helpTitle = selectedHelpTitle
    }
  }
  
  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
  
  // MARK: - IBActions
  
  
  
  // MARK: - Public methods
  
  
}

extension HelpListViewController: UITableViewDataSource {
  func numberOfSections(in tableView: UITableView) -> Int {
    return 1
  }
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if let count = helpTitles?.count, count > 0 {
      return count
    } else {
      return 1
    }
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let count = helpTitles?.count, count > 0 {
      let cell = tableView.dequeueReusableCell(withIdentifier: helpCellIdentifier, for: indexPath) as! HelpCell
      let helpTitle = helpTitles![indexPath.row]
      cell.helpTitle.text = helpTitle.title
      return cell
    } else {
      let cell = tableView.dequeueReusableCell(withIdentifier: noDataCellIdentifier, for: indexPath) as! NoDataTableCell
      cell.tipLabel.text = "没有帮助数据"
      return cell
    }
  }
}

extension HelpListViewController: UITableViewDelegate {
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let count = helpTitles?.count, count > 0 {
      selectedHelpTitle = helpTitles![indexPath.row]
      performSegue(withIdentifier: "ShowHelpDetailSegue", sender: nil)
    }
    tableView.deselectRow(at: indexPath, animated: true)
  }
  
  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if let count = helpTitles?.count, count > 0 {
      return HelpCell.defaultHeight
    } else {
      return NoDataTableCell.defaultHeight
    }
  }
}
